package com.sixiaolong.controller;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sixiaolong.common.BaseContext;
import com.sixiaolong.common.R;
import com.sixiaolong.domain.User;
import com.sixiaolong.service.UserService;
import com.sixiaolong.utils.SendSms;
import com.sixiaolong.utils.ValidateCodeUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Map;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;
//    @Resource
//    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 发送手机验证码
     * @param user
     * @return
     */
    @PostMapping("/sendMsg")
    public R<String> sendMsg(@RequestBody User user, HttpSession httpSession){
//        获取手机号
        String phone = user.getPhone();
        if (Strings.isNotEmpty(phone)){
            //        随机生成验证码
            String code = "1234";
//            String code = ValidateCodeUtils.generateValidateCode(4).toString();
//        调用阿里云提供的短信服务api完成发送短信
//            SendSms.sendMessage("阿里云短信测试","SMS_154950909",phone,code);
//            将生成的验证码保存到session中
//            httpSession.setAttribute(phone,code);
            redisTemplate.opsForValue().set(phone,code,5, TimeUnit.MINUTES);
            System.out.println(redisTemplate.opsForValue().get(phone));
            return R.success("手机验证码发送成功");
        }

        return R.error("短信发送失败");
    }

    /**
     *  登录
     * @param map
     * @param httpSession
     * @return
     */
    @PostMapping("/login")
    public R<User> login(@RequestBody Map map, HttpSession httpSession){
//        Object phone = map.get("phone");  如果从session中取到的验证码和 从前台拿到的验证码一致
        if (redisTemplate.opsForValue().get( map.get("phone").toString())!=null && redisTemplate.opsForValue().get( map.get("phone").toString()).equals(map.get("code").toString())){
//            说明登陆成功   判断当前用户是否是新用户  是的话 就需要插入
//            登录成功后需要移除 redis缓存的该数据
            redisTemplate.delete(map.get("phone").toString());
            LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
            lqw.eq(User::getPhone,map.get("phone").toString());
            User user = userService.getOne(lqw);
            if (user != null){
                httpSession.setAttribute("user",user.getId());
                return R.success(user);
            }
            User user1 = new User();
            user1.setPhone(map.get("phone").toString());
            userService.save(user1);
            httpSession.setAttribute("user",user.getId());
            return R.success(user);
        }
        return R.error("登录失败");
    }

    //    退出登录
        @PostMapping("/loginout")
        public R<String> logout(HttpSession httpSession){
            httpSession.removeAttribute("user");
            return R.success("退出成功");
        }
}
